package org.wyn.mongodb.dao.impl;

import org.wyn.mongodb.bean.User;
import org.wyn.mongodb.bean.VisitHistory;
import org.wyn.mongodb.common.SearchBDProvider;

import com.google.code.morphia.dao.BasicDAO;
/**
 * 用户互访历史记录 mongodb dao实现层
 * @author wyn
 *
 */
public class VisitHistoryDao extends BasicDAO<VisitHistory,String>{
	public VisitHistoryDao(){
		super(SearchBDProvider.getInstance());
	}
	/**
	 * 为用户userName创建记录
	 * @param userName
	 * @return
	 */
	public boolean createHistory(String userName){
		save(new VisitHistory(userName));
		ensureIndexes();
		return true;
	}
	public static void main(String[] args) {
		/**
		 * 遍历user表创建数据
		 */
		UserDao uDao = new UserDao();
		VisitHistoryDao vDao = new VisitHistoryDao();
		for(User user : uDao.createQuery()){
			VisitHistory vh = vDao.findOne("userName", user.getUserName());
			if(null == vh){
				vh = new VisitHistory(user.getUserName());
				vDao.save(vh);
			}
		}
		vDao.ensureIndexes();
	}
}
